package com.sc.pattern.guardedsuspension;

import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID;

import java.util.Random;

/**
 * Created  on 2018/1/7.
 *
 * @author sc
 */
public class ServerThread extends Thread {

    private final RequestQueue requestQueue;
    private final Random random;
    private volatile  boolean flag = true;
    public ServerThread(RequestQueue requestQueue){
        this.requestQueue =requestQueue;
        random = new Random(System.currentTimeMillis());
    }

    @Override
    public void run() {
        while (flag){
            Request request = requestQueue.getRequest();
            if(request==null){
                System.out.println("get empty");
                continue;
            }
            System.out.println("server get  "+request.getUrl());
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
                return;
            }
        }
    }

    public void close(){
        this.flag =false;
        this.interrupt();
    }
}
